package com.shycloud.mido.homework.service.impl;

import com.shycloud.mido.homework.api.dto.WeekDataDto;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.homework.api.entity.WeekDataMember;
import com.shycloud.mido.homework.api.entity.WeekMain;
import com.shycloud.mido.homework.api.vo.WeekDataDayVO;
import com.shycloud.mido.homework.mapper.WeekDataMemberMapper;
import com.shycloud.mido.homework.mapper.WeekMainMapper;
import com.shycloud.mido.homework.service.WeekDataMemberService;
import com.shycloud.mido.member.api.entity.MemberActivateEntity;
import com.shycloud.mido.member.api.feign.RemoteMemberService;
import lombok.AllArgsConstructor;

/**
 * <p>
 * 周报数据周维度表 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2021-06-15
 */
@Service
@AllArgsConstructor
public class WeekDataMemberServiceImpl extends ServiceImpl<WeekDataMemberMapper, WeekDataMember> implements WeekDataMemberService {

	/** 周报主表 mapper */
	private WeekMainMapper weekMainMapper;
	/** 周报用户 mapper */
	private WeekDataMemberMapper weekDataMemberMapper;

	/** 链接 member业务 feign */
	private RemoteMemberService remoteMemberService;

	@Override
	public Map<String, Object> getWeekAllTime(String memberId, String studentId, String weekId) {

		//创建返回数据响应体
		Map<String, Object> resMap = new HashMap<>(2);
		//查询周报主表
		WeekMain weekMain = weekMainMapper.selectById(weekId);
		//非空判断
		if (weekMain == null) {
			throw new BusinessException("weekId查询不到对应周报");
		}
		//处理本周时间
		LocalDate startWeekDate = weekMain.getStartWeekDate();
		LocalDate endWeekDate = weekMain.getEndWeekDate();
		LocalDateTime startTime = startWeekDate.atTime(0, 0, 0, 0);
		LocalDateTime endTime = endWeekDate.atTime(23, 59, 59);
		//查询周练琴总时长（单位【分钟】）
		List<WeekDataDto> playDataDay = baseMapper.getPlayDataDay(weekId, studentId, startTime, endTime, null, null, null);
		Double totalTime = playDataDay.stream().collect(Collectors.summingDouble(WeekDataDto::getPlayTime));
		resMap.put("totalTime", totalTime);

		//根据 weekId , memberId 获取此学生排名
		Integer totalTimeRank = remoteMemberService.getTotalTimeRank(weekId, memberId);

		if (totalTimeRank == null) {
			totalTimeRank = 0;
		}

		if (totalTimeRank == 1) {
			resMap.put("totalTimePercentage", new BigDecimal(100));
			return resMap;
		}

		if (totalTime == 0d) {
			resMap.put("totalTimePercentage", new BigDecimal(0));
			return resMap;
		}

		Integer count = weekDataMemberMapper.selectCount(new QueryWrapper<WeekDataMember>()
				.eq("week_id", weekId));

		BigDecimal totalTimePercentage =
				new BigDecimal(count - totalTimeRank).divide(new BigDecimal(count), 2, RoundingMode.HALF_UP);

		resMap.put("totalTimePercentage", totalTimePercentage.multiply(new BigDecimal(100)));

		return resMap;
	}

	@Override
	public IPage<WeekDataMember> pageMemberListByWeek(Page<WeekDataMember> page) {
		return baseMapper.pageMemberListByWeekV3(page);
	}

	@Override
	public MemberActivateEntity getMidoMasterByMemberAndWeek(String memberId) {
		return baseMapper.getMidoMasterByMemberAndWeek(memberId);
	}
}
